//
//  XFStatus.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/20.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

class XFStatus: NSObject {

    //MARK: － 属性
    
    /// 创建时间
    var created_at: String?
    
    /// 微博id
    var id: Int64 = 0
    
    /// 微博正文内容
    var text: String?
    
    /// 来源
    var source: String?
    
    /// 微博的配图
    var pic_urls: [[String : AnyObject]]? {
        
        didSet{
            
            let count = pic_urls?.count ?? 0
            
            if count == 0 {
                return
            }
            
            storePictureURLs = [NSURL]()
            
            for dict in pic_urls! {
                let urlString = dict["thumbnail_pic"] as! String
                
                let url = NSURL(string: urlString)!
                
                storePictureURLs?.append(url)
            }
            
        }

    }
    
    
    /// 微博配图的NSURL数组
    var storePictureURLs:[NSURL]?
    
    var pictureURLs:[NSURL]? {
        
        return retweeted_status == nil ? storePictureURLs : retweeted_status?.storePictureURLs
    }
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数(赞)
    var attitudes_count: Int = 0
    
    /// 用户模型
    var user: XFUser?
    
    var retweeted_status: XFStatus?
    
    /// 行高
    var rowHeight: CGFloat?
    
    func cellID() -> String {
        
        return retweeted_status == nil ? XFStatusCellReuseIdentifier.StatusNormalCell.rawValue : XFStatusCellReuseIdentifier.StatusForwardCell.rawValue
    }
    
    
    
    // 字典转模型
    init(dict: [String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    override var description: String {
        let prop = ["created_at", "id", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count"]
        
        // 通过一个数组来生成一个字典,数组中的元素,会成为字典的key,key所对应的value，会从模型中来找对应的属性值
        let dict = dictionaryWithValuesForKeys(prop)
        
        // \n 换行, \t table键
        return "\n\t微博模型: \(dict)"
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        print("key: \(key)")
        // 判断如果是user这个key,我们自己来字典转模型,不需要kvc
        if key == "user" {
            // 我们自己来字典转模型
            if let dict = value as? [String: AnyObject] {
                let userModel = XFUser(dict: dict)
                
                // 将模型赋值赋值给user属性
                self.user = userModel
            }
            
            // 一定要记得return,不然会调用父类的setValue,会被覆盖
            return
        }
        
        if key == "retweeted_status" {
            
            if let dict = value as? [String :AnyObject] {
                
                retweeted_status = XFStatus(dict: dict)
            }
        
            return
        }
        
        
        super.setValue(value, forKey: key)
    }
    
    //MARK: - 加载微博数据
    class func xxLoadStatus(finished: (statuses:[XFStatus]? ,error: NSError?) -> ()) {
        
        XFNetworkTool.sharedInstance.loadStatus { (result, error) -> () in
            
            if error != nil || result == nil {
                
                finished(statuses: nil, error: error)
                return
            }
            
            if let array = result?["statuses"] as? [[String :AnyObject]] {
                
                var statuses = [XFStatus]()
                
                for dict in array {
                    
                    let status = XFStatus(dict: dict)
                    
                    statuses.append(status)
                }
                
                finished(statuses: statuses, error: nil)
                
            }else {
                
                finished(statuses: nil, error: nil)
            }
        }
    }
}
